import { Success } from '../../code/http-exception';
import auth from '../../middlewares/auth';
import User from '../../sql/module/user/user';
import Menu from '../../sql/module/menu';
import Role from '../../sql/module/user/role';
import RoleMenu from '../../sql/module/_dictionaries/role_menu';
import UserRole from '../../sql/module/_dictionaries/user_role';
import { sequelize } from '../../sql/db';
const visitor = [1, 37, 38, 42, 43, 45, 46, 49, 71, 72, 75, 76, 77, 78, 80, 87, 89, 88, 81, 95]
const main = [30, 31, 32, 33, 34, 35, 36, 39, 52, 53, 54, 55, 56, 57, 58, 59, 61, 65, 68, 69, 70, 73, 74, 79, 84, 85, 86, 37, 42]
const user = [1, 29, 37, 39, 42, 60, 62, 63, 64, 66, 71, 72, 73, 82, 83, 86, 90]
const DIY = [30, 91]
const CME = [30, 92, 93, 94]
const admin = [2, 3, 5, 18, 20, 21, 19, 22, 25, 23, 24, 6, 7, 8]
const router: Api = new Map()

const resetMenu = async () => {
    const menus = [
        { id: 1, label: '主页', url: '_index', type: 1, img: 'wap-home-o' },
        { id: 2, label: '账号', url: '_user', type: 1, img: 'wap-home-o' },
        { id: 3, label: '账号管理', url: 'user', type: 1, upLevel: '_user' },
        { id: 4, label: '权限管理', url: 'power', type: 1, upLevel: '_user' },
        { id: 5, label: '区域管理', url: 'area', type: 1, upLevel: '_user' },
        {
            id: 6,
            label: '区域列表', url: 'areaList', type: 2, upLevel: 'area',
            img: '/srcm/assets/images/icon_kaijuchufang.png'
        },
        { id: 7, label: '添加区域', url: 'addArea', type: 3, upLevel: 'areaList' },
        { id: 8, label: '删除', url: 'deleteArea', type: 3, upLevel: 'areaList' },
        {
            id: 9,
            label: '菜单列表', url: 'menu', type: 2, upLevel: 'power',
            img: '/srcm/assets/images/icon_guahaozixun.png'
        },
        { id: 10, label: '编辑', url: 'editMenu', type: 3, upLevel: 'menu' },
        { id: 11, label: '启用/禁用', url: 'menuState', type: 3, upLevel: 'menu' },
        { id: 12, label: '重置菜单', url: 'resetMenu', type: 3, upLevel: 'menu' },
        {
            id: 13,
            label: '身份列表', url: 'rolelist', type: 2, upLevel: 'power',
            img: '/srcm/assets/images/icon_kaijuchufang.png'
        },
        { id: 14, label: '添加身份', url: 'addRole', type: 3, upLevel: 'rolelist' },
        { id: 15, label: '重置身份', url: 'resetRole', type: 3, upLevel: 'rolelist' },
        { id: 16, label: '编辑', url: 'updateRole', type: 3, upLevel: 'rolelist' },
        { id: 17, label: '删除', url: 'deleteRole', type: 3, upLevel: 'rolelist' },
        {
            id: 18,
            label: '账号列表', url: 'userList', type: 2, upLevel: 'user',
            img: '/srcm/assets/images/icon_kaijuchufang.png'
        },
        { id: 19, label: '添加账号', url: 'addUser1', type: 3, upLevel: 'userList' },
        {
            id: 20,
            label: '添加账号', url: 'addUser2', type: 2, upLevel: 'user',
            img: '/srcm/assets/images/icon_kaijuchufang.png'
        },
        { id: 21, label: '详情', url: 'userDetail2', type: 3, upLevel: 'userList' },
        { id: 22, label: '重置密码', url: 'resetPassword', type: 3, upLevel: 'userList' },
        { id: 23, label: '编辑', url: 'userDetail1', type: 3, upLevel: 'userList' },
        { id: 24, label: '编辑', url: 'updataUser', type: 3, upLevel: 'userDetail2' },
        { id: 25, label: '删除', url: 'deleteUser', type: 3, upLevel: 'userList' },
        {
            id: 26, label: '反馈列表', url: 'suggestList', type: 2, upLevel: '_user',
            img: '/srcm/assets/images/icon_kaijuchufang.png'
        },
        { id: 27, label: '删除', url: 'deleteRecord', type: 3, upLevel: 'recordList' },
        { id: 28, label: '记录详情', url: 'recordDetail', type: 3, upLevel: 'recordList' },
        { id: 29, label: '销售', url: '_sale', type: 1, img: 'wap-home-o' },
        { id: 30, label: '工厂', url: '_factory', type: 1, img: 'wap-home-o' },
        {
            id: 31, label: '类型', url: 'materialType', type: 2, upLevel: 'materials',
            img: '/srcm/assets/images/icon_kaijuchufang.png'
        },
        {
            id: 32, label: '材料列表', url: 'materiaList', type: 2, upLevel: 'materials',
            img: '/srcm/assets/images/icon_kaijuchufang.png'
        },
        { id: 33, label: '添加材料', url: 'addMaterial', type: 3, upLevel: 'materiaList' },
        { id: 34, label: '删除', url: 'deleteMaterial', type: 3, upLevel: 'materiaList' },
        { id: 35, label: '修改', url: 'updateMaterial', type: 3, upLevel: 'materiaList' },
        { id: 36, label: '启用/禁用', url: 'changeMaterialState', type: 3, upLevel: 'materiaList' },
        { id: 37, label: '个人页', url: '_log', type: 1, public: 1, img: "user-o" },
        { id: 38, label: '个人中心', url: 'account', type: 2, upLevel: '_log', public: 1 },
        { id: 39, label: '个人设置', url: 'basicset', type: 2, upLevel: '_log', public: 1 },
        { id: 40, label: '编辑', url: 'changeInfo', type: 3, upLevel: 'basicset', public: 1 },
        { id: 41, label: '修改密码', url: 'changePassword', type: 3, upLevel: 'basicset', public: 1 },
        { id: 42, label: '未读消息', url: 'message', type: 2, upLevel: '_log', public: 1, isPhone: 1 },
        { id: 43, label: '问题反馈与意见', url: 'suggest', type: 2, upLevel: '_log', public: 1 },
        {
            id: 44,
            label: '记录列表', url: 'recordList', type: 2, upLevel: '_user',
            img: '/srcm/assets/images/icon_kaijuchufang.png'
        },
        { id: 45, label: '我的反馈', url: 'suggestSelfList', type: 3, upLevel: 'suggest', public: 1 },
        { id: 46, label: '详情', url: 'suggestSelfInfo', type: 3, upLevel: 'suggestSelfList', public: 1 },
        { id: 47, label: '详情', url: 'suggestInfo', type: 3, upLevel: 'suggestList' },
        { id: 48, label: '回复', url: 'addSuggest', type: 3, upLevel: 'suggestInfo' },
        { id: 49, label: '删除', url: 'suggestDelete', type: 3, upLevel: 'suggestSelfList', public: 1 },
        { id: 50, label: '恢复', url: 'recover', type: 3, upLevel: 'recordDetail' },
        { id: 51, label: '私信', url: 'C2C', type: 3, upLevel: 'account', public: 1 },
        { id: 52, label: '仓库管理', url: 'store', type: 1, upLevel: '_factory' },
        {
            id: 53, label: '类型', url: 'templateType', type: 2, upLevel: 'store',
            img: '/srcm/assets/images/icon_kaijuchufang.png'
        },
        {
            id: 54, label: '模版', url: 'template', type: 2, upLevel: 'store',
            img: '/srcm/assets/images/icon_kaijuchufang.png'
        },
        {
            id: 55, label: '仓库', url: 'itemList', type: 2, upLevel: 'store',
            img: '/srcm/assets/images/icon_kaijuchufang.png'
        },
        { id: 56, label: '启用/禁用', url: 'changeTemplateState', type: 3, upLevel: 'template' },
        { id: 57, label: '添加产品', url: 'addItem', type: 3, upLevel: 'template' },
        { id: 58, label: '添加模版', url: 'addTemplateItem', type: 3, upLevel: 'template' },
        { id: 59, label: '删除', url: 'deleteItem', type: 3, upLevel: 'itemList' },
        {
            id: 60, label: '仓库', url: 'saleStore', type: 2, upLevel: '_sale',
            img: '/srcm/assets/images/icon_kaijuchufang.png'
        },
        { id: 61, label: '加入出货单', url: 'addList', type: 3, upLevel: 'itemList' },
        { id: 62, label: '加入出货单', url: 'addSaleList', type: 3, upLevel: 'saleStore' },
        {
            id: 63, label: '出货单', url: 'outProcureList', type: 2, upLevel: '_sale',
            img: '/srcm/assets/images/icon_kaijuchufang.png'
        },
        {
            id: 64, label: '进货单', url: 'inProcureList', type: 2, upLevel: '_sale',
            img: '/srcm/assets/images/icon_kaijuchufang.png'
        },
        { id: 65, label: '模版', url: 'itemInfo', type: 3, upLevel: 'template' },
        { id: 66, label: '详情', url: 'storeInfo', type: 3, upLevel: 'saleStore' },
        { id: 68, label: '出货单', url: 'outProcureList', type: 3, upLevel: 'itemList' },
        { id: 69, label: '详情', url: 'storeInfo', type: 3, upLevel: 'itemList' },
        { id: 70, label: '材料管理', url: 'materials', type: 1, upLevel: '_factory' },
        { id: 71, label: '主页', url: 'index', type: 2, upLevel: '_index' },
        { id: 72, label: '详情', url: 'indexDetail', type: 3, upLevel: 'index' },
        { id: 73, label: '设置可流转账号', url: 'changeLink', type: 3, upLevel: 'basicset' },
        { id: 74, label: '设置图片', url: 'updateImg', type: 3, upLevel: 'template' },
        { id: 75, label: '添加产品', url: 'userAddItem', type: 3, upLevel: 'account', public: 1 },
        { id: 76, label: '我的产品', url: 'storeSelf', type: 2, upLevel: '_log' },
        { id: 77, label: '产品详情', url: 'storeSelfInfo', type: 3, upLevel: 'storeSelf' },
        { id: 78, label: '扫码', url: 'scan', type: 3, upLevel: '_log', public: 1, isPhone: 1 },
        { id: 79, label: '评论列表', url: 'comments', type: 3, upLevel: 'template' },
        { id: 80, label: '加入订单', url: 'addIndex', type: 3, upLevel: 'indexDetail' },
        { id: 81, label: '订单列表', url: 'checkList', type: 3, level: 1 },
        { id: 82, label: '未确认订单', url: 'unConfirmList', type: 3, level: 1 },
        {
            id: 83, label: '历史订单', url: 'history', type: 2, upLevel: '_sale',
            img: '/srcm/assets/images/icon_kaijuchufang.png'
        },
        { id: 84, label: '全部商品', url: 'allStores', type: 3, upLevel: 'template' },
        { id: 85, label: '详情', url: 'storeInfo', type: 3, upLevel: 'allStores' },
        { id: 86, label: '未确认订单', url: 'unConfirmList', type: 3, upLevel: 'message', isPhone: 1 },
        { id: 87, label: '申请售后', url: 'createGroup', type: 3, upLevel: 'storeSelf' },
        { id: 88, label: '聊天', url: 'groupList', type: 3, public: 1 },
        { id: 89, label: '预定', url: 'orderVisitor', type: 3, upLevel: 'indexDetail' },
        { id: 90, label: '预定(商家)', url: 'order', type: 3, upLevel: 'indexDetail' },
        {
            id: 91, label: 'DIY', url: 'diy', type: 2, upLevel: '_factory',
            img: '/srcm/assets/images/icon_kaijuchufang.png'
        },
        {
            id: 92, label: '全屋定制', url: 'customize', type: 2, upLevel: '_factory',
            img: '/srcm/assets/images/icon_kaijuchufang.png'
        },
        { id: 93, label: '添加案例', url: 'addCustomize', type: 3, upLevel: 'customize' },
        { id: 94, label: '预定列表', url: 'customizeList', type: 3, upLevel: 'customize' },
        { id: 95, label: '咨询', url: 'addC2CL', type: 3, upLevel: 'indexDetail' },
        { id: 96, label: 'C2C', url: 'C2CList', type: 3, public: 1 }
    ]
    await Promise.all(menus.map(async item => {
        const res = await Menu.update({ isPhone: 0, icon: '', img: '', public: null, ...item }, { where: { id: item.id } })
        if (!res[0]) await Menu.create(item)
    }))
}
const resetRole = async () => {
    await Role.create({
        id: 1,
        uid: 100,
        name: 'superAdmin'
    })
    await Role.bulkCreate([{
        id: 2,
        uid: 1,
        name: 'visitor',
    }, {
        id: 3,
        uid: 2,
        name: 'user',
    }, {
        id: 4,
        uid: 3,
        name: 'main',
    }, {
        id: 5,
        uid: 99,
        name: 'admin',
    }, {
        id: 6,
        uid: 4,
        name: 'DIY',
    }, {
        id: 7,
        uid: 5,
        name: 'CME',
    }])
    await RoleMenu.bulkCreate(visitor.map(item => ({
        MenuId: item,
        RoleId: 2
    })))
    await RoleMenu.bulkCreate(user.map(item => ({
        MenuId: item,
        RoleId: 3
    })))
    await RoleMenu.bulkCreate(main.map(item => ({
        MenuId: item,
        RoleId: 4
    })))
    await RoleMenu.bulkCreate(admin.map(item => ({
        MenuId: item,
        RoleId: 5
    })))
    await RoleMenu.bulkCreate(DIY.map(item => ({
        MenuId: item,
        RoleId: 6
    })))
    await RoleMenu.bulkCreate(CME.map(item => ({
        MenuId: item,
        RoleId: 7
    })))
}
const setAdmin = async () => {
    const hasAdmin = await UserRole.findOne({
        where: {
            RoleId: 1
        }
    })
    if (!hasAdmin) {
        const [user] = await User.findOrCreate({
            where: {
                phone: 'admin'
            }, defaults: {
                phone: 'admin',
                password1: 'admin',
                nickname: '系统管理员'
            }
        })
        await UserRole.create({
            UserId: user.id,
            RoleId: 1
        })
    }
}
//type 菜单1，路由地址2，按钮3
router.set('/resetMenu', [auth('resetMenu'), async () => {
    await resetMenu()
    throw new Success()
}])

router.set(['/resetRole', 'get'], [async () => {
    await resetRole()
    throw new Success()
}])

router.set('/admin', async () => {
    await setAdmin()
    throw new Success()
})
//init时打开
router.set(['/init', 'get'], async () => {
    await sequelize.sync()
    await resetMenu()
    await resetRole()
    await setAdmin()
    throw new Success()
})

router.set(['/sync', 'get'], async () => {
    await sequelize.sync()
    throw new Success()
})

export { router }